#include "Game.h"

IGame* createGame( HINSTANCE hInst, HWND hWnd )
{
	return new Game( hInst, hWnd );
}

Game::Game( HINSTANCE hInst, HWND hWnd ) :
	m_input( hInst, hWnd )
{
	// initialize camera
	m_camera.hand = Camera::RIGHTHANDED;

	m_camera.eye = float3( 0, 0, 5.f );
	m_camera.at  = float3( 0, 0, 0 );
	m_camera.up  = float3( 0, 1, 0 );

	m_camera.projectionType = Camera::PERSPECTIVE_FOV;
	m_camera.perspectiveFov.fovy = D3DXToRadian(45.f);
	m_camera.perspectiveFov.aspect = 1024.f/768.f;

	m_camera.zn = 1.0f;
	m_camera.zf = 100.0f;

	// initialize ground
	m_ground.invLocalInertia = zero33();
	m_ground.invMass = 0;
}

Game::~Game()
{
	m_d3dDevice = 0;
}

void Game::onCreateGraphics( D3DDevice device )
{
	m_d3dDevice = device;

	m_d3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_CW );

	float44 world = identity44();
	float44 view = m_camera.view();
	float44 proj = m_camera.projection();

	m_d3dDevice->SetTransform( D3DTS_WORLD, &world );
	m_d3dDevice->SetTransform( D3DTS_VIEW, &view );
	m_d3dDevice->SetTransform( D3DTS_PROJECTION, &proj );

	//
	poly::Polygon polygon;
	polygon.push_back( float3( 0, -0.1, 0 ) );
	polygon.push_back( float3( 1, 0, 0 ) );
	polygon.push_back( float3( 0, +0.1, 0 ) );

	m_bar[0].setup( m_d3dDevice, polygon, float4( 0.f, 1.f, 0.5f, 0.2f ) );
	m_bar[1].setup( m_d3dDevice, polygon, float4( 0.f, 0.5f, 1.0f, 0.2f ) );
	m_bar[1].rigidBody.orientation = rotationAxis( float3(0, 0, 1), D3DXToRadian(30) );
	m_bar[2].setup( m_d3dDevice, polygon, float4( 0.f, 0.5f, 0.2f, 1.0f ) );

	m_spring.rigidBody1 = &m_ground;
	m_spring.rigidBody2 = &m_bar[0].rigidBody;
	m_spring.pointOfApp1 = zero3();
	m_spring.pointOfApp2 = zero3();
	m_spring.forceDensity = 50;
	m_spring.friction = 10;
	
	m_joint[0].rigidBody1 = &m_ground;
	m_joint[0].rigidBody2 = &m_bar[0].rigidBody;
	m_joint[0].pointOfApp1 = zero3();
	m_joint[0].pointOfApp2 = zero3();

	m_joint[1].rigidBody1 = &m_bar[0].rigidBody;
	m_joint[1].rigidBody2 = &m_bar[1].rigidBody;
	m_joint[1].pointOfApp1 = float3(1, 0, 0);
	m_joint[1].pointOfApp2 = zero3();

	m_joint[2].rigidBody1 = &m_bar[1].rigidBody;
	m_joint[2].rigidBody2 = &m_bar[2].rigidBody;
	m_joint[2].pointOfApp1 = float3(1, 0, 0);
	m_joint[2].pointOfApp2 = zero3();

	m_joint[3].rigidBody1 = &m_bar[2].rigidBody;
	m_joint[3].rigidBody2 = &m_ground;
	m_joint[3].pointOfApp1 = float3(1, 0, 0);
	m_joint[3].pointOfApp2 = float3(1.5f, 0, 0);

	m_dynamics.constraints.insert( &m_spring );
	//m_dynamics.constraints.insert( &m_joint[0] );
	m_dynamics.constraints.insert( &m_joint[1] );
	m_dynamics.constraints.insert( &m_joint[2] );
	m_dynamics.constraints.insert( &m_joint[3] );

	m_dynamics.dynObjects.insert( &m_bar[0] );
	m_dynamics.dynObjects.insert( &m_bar[1] );
	m_dynamics.dynObjects.insert( &m_bar[2] );
}

void Game::onResetGraphics( D3DDevice device )
{
	m_d3dDevice = device;

	m_d3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_CW );

	float44 view = m_camera.view();
	float44 proj = m_camera.projection();

	m_d3dDevice->SetTransform( D3DTS_VIEW, &view );
	m_d3dDevice->SetTransform( D3DTS_PROJECTION, &proj );
}

void Game::onLostGraphics()
{
}

void Game::onFrameMove(float dt)
{
	m_dynamics.step( dt );
	
	// ad hoc stuff
	if( GetAsyncKeyState( VK_LEFT ) & 0x8000 )
		m_bar[0].rigidBody.addTorque(float3(0,0,5.f));
	if( GetAsyncKeyState( VK_RIGHT ) & 0x8000 )
		m_bar[0].rigidBody.addTorque(float3(0,0,-5.f));

}

void Game::onFrameRender(float dt)
{
	float44 world = identity44();
	m_dynamics.render( dt );
}

void Game::onDestroyGraphics()
{
}
